import type { ApplicationModel } from '@/apis/model/application'
import type { BandwidthType } from '@/apis/model/bandwidth'
import { getApplicationsApi } from '@/apis/modules/application'
import { getBandwidthTypeListApi } from '@/apis/modules/bandwidthType'

// 区域
export function useApplication() {
  const application = ref<Array<ApplicationModel>>([])
  /** 应用列表 */
  function getApplication() {
    getApplicationsApi({
      limit: 0,
    }).then((res) => {
      application.value = res.list as Array<ApplicationModel>
    })
  }
  const applicationOptions = computed<Array<OptionItem>>(() => {
    return application.value.map((item) => {
      return {
        label: item?.name as string,
        value: item?.app_id,
      }
    })
  })

  const bandWidthDataList = ref<Array<BandwidthType>>([])
  /** 带宽数据 */
  function getBandWidth() {
    getBandwidthTypeListApi({}).then((res) => {
      bandWidthDataList.value = (res.list as Array<BandwidthType>) || []
    })
  }

  const { parse } = useLangField()

  const bandWidthOptions = computed<Array<OptionItem>>(() => {
    return bandWidthDataList.value.map((item) => {
      const field = parse(item.name || '')
      return {
        label: field?.['zh-CN'] || '-',
        value: item?.id,
      }
    })
  })

  onMounted(() => {
    getApplication()
    getBandWidth()
  })
  return {
    applicationOptions,
    application,
    bandWidthDataList,
    bandWidthOptions,
  }
}
